﻿using System;

namespace ALDIOrganizer
{
    [Serializable]
    public enum RowTagType { LongInt, GUID, All };
    
    [Serializable]
    public class RowTag
    {
        public RowTagType MyType;
        private Guid _guidValue;
        private Int64 _int64Value;
        public Guid GuidValue
        {
            get
            {
                return _guidValue;
            }
        }
        public Int64 Int64Value
        {
            get
            {
                return _int64Value;
            }
        }

        internal bool Match(SheetType pMode)
        {
            if (MyType == RowTagType.GUID)
            {
                return (pMode == SheetType.Template);
            }
            if (MyType == RowTagType.LongInt)
            {
                return (pMode == SheetType.Rota);
            }
            return false;
        }

        public void SetValue(Guid pGuidValue)
        {
            _guidValue = pGuidValue;
            MyType = RowTagType.GUID;
        }

        public void SetValue(Int64 pInt64Value)
        {
            _int64Value = pInt64Value;
            MyType = RowTagType.LongInt;
        }

        public override bool Equals(object obj)
        {
            if (obj.GetType() != typeof(RowTag))
            {
                return false;
            }            
            var anotherRowTag = (RowTag)obj;

            if (MyType != anotherRowTag.MyType)
            {
                return false;
            }
            if (MyType == RowTagType.GUID)
            {
                return _guidValue.Equals(anotherRowTag._guidValue);
            }
            return MyType == RowTagType.LongInt ?
                _int64Value.Equals(anotherRowTag._int64Value) :
                base.Equals(obj);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                var result = MyType.GetHashCode();
                result = (result * 397) ^ _guidValue.GetHashCode();
                result = (result * 397) ^ _int64Value.GetHashCode();
                return result;
            }
        }

        public RowTag(Guid guid)
        {
            SetValue(guid);
        }

        public RowTag(Int64 int64)
        {
            SetValue(int64);
        }

        public static RowTag GetNewGuidTag
        {
            get
            {
                return new RowTag(Guid.NewGuid());
            }
        }                

    }
}
